import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

// Diese Klasse wurde anhand eines Bsp. implementiert/umgeschrieben (Quelle: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/)
public class XmlToGraphParser {
	private SAXParserFactory factory;
	private SAXParser saxParser;
	private DefaultHandler handler;
	private Graph ret;
    
    public XmlToGraphParser() {
        try {
        	factory	= SAXParserFactory.newInstance();
			saxParser = factory.newSAXParser();
			ret= new Graph();
			handler = new DefaultHandler() {
				 
				 int aktId=0;
				 int aktSrcid=0;
			     boolean bId = false;
			     boolean bCap = false;
			     boolean bSrcid = false;
			     boolean bDstid = false;
			 
			     public void startElement(String uri, String localName,
			        String qName, Attributes attributes)
			        throws SAXException {
			 
			        if (qName.equalsIgnoreCase("ID")) {
			           bId = true;
			        }
			 
			        if (qName.equalsIgnoreCase("CAPACITY")) {
			           bCap = true;
			        }
			 
			        if (qName.equalsIgnoreCase("srcid")) {
			           bSrcid = true;
			        }
			 
			        if (qName.equalsIgnoreCase("dstid")) {
			           bDstid = true;
			        }
			 
			     }
			 
			     public void endElement(String uri, String localName,
			          String qName)
			          throws SAXException {}
			 
			     public void characters(char ch[], int start, int length)
			         throws SAXException {
			        
			        
					if (bId) {
//			            System.out.println("ID : "
//			                + new String(ch, start, length));
			        	aktId = Integer.valueOf(new String(ch, start, length));
			        	 
			            bId = false;
			          }
			 
			          if (bCap) {
//			              System.out.println("Capacity : "
//			                  + new String(ch, start, length));
			        	  int cap = Integer.valueOf(new String(ch, start, length));
			        	  ret.addKn(new Knoten(aktId, cap));
			              bCap = false;
			           }
			 
			          if (bSrcid) {
//			              System.out.println("Edge : "
//			                  + new String(ch, start, length));
				          aktSrcid = Integer.valueOf(new String(ch, start, length));

			              bSrcid = false;
			           }
			 
			          if (bDstid) {
//			              System.out.println("Salary : "
//			                  + new String(ch, start, length));
			        	  int Dtsid = Integer.valueOf(new String(ch, start, length));
//			        	  ret.addKn(kn)
			        	  ret.getKn(Dtsid).addPre(ret.getKn(aktSrcid));
			              bDstid = false;
			           }
			 
			        }
			 
			      };
        }catch (Exception e) {} // should not happen
		
	}
    public Graph getGraph(File f){
    	 try {
			saxParser.parse(f, handler);
		} catch (SAXException e) {} catch (IOException e) {} // should not happen
		
		return ret;
    }
}
